<?php

require_once("adrese.class.php");

class rokovnik_report extends page{
	
	var $db = null;
	var $rokovnik = null;
	var $filter;
	
	var $adrese;
	
	function rokovnik_report($rokovnik,$get,$post){
		$this->loadConfig();
		$this->rokovnik = $rokovnik;
		$this->db = new db_functions();
		$this->adrese = new adrese();
		
		$this->filter = array_merge($post,$get);
		
		$action = isset($get["t"])?$get["t"]:$post["t"];
		
		switch ($action){
			case "tm":
				$this->reportTelemarketing();
				break;
			case "tmMore":
				$this->reportTelemarketingMoreInfo($get["id"]);
				break;
			case "tm1":
				$this->reportTelemarketingZakazani();
				break;
			case "man":
				$this->reportManager();
				break;
			case "kom":
				$this->reportKomercijalista();
				break;
			case "kom1":
				$this->exportKomercijalistaNezvani();
				break;
		}
	}
	function reportTelemarketingMoreInfo($id){
		
		$adrese = $delatnosti = array();
		
		$q1 = "SELECT komitent_adresa FROM komitenti WHERE komitent_id=$id";
		$adrese[] = $this->db->getOne($q1);
		
		$q2 = "SELECT kd.komdel_adresa_id, d.delatnost_ime "
			."FROM komitenti_delatnosti kd, delatnosti d "
			."WHERE kd.komdel_del_id=d.delatnost_id "
			."AND kd.komdel_kom_id=$id";
		$res2 = $this->db->select2($q2);
		if($res2 !== false){
			foreach ($res2 as $row){
				$adr = $row["komdel_adresa_id"];
				$delnaz = $row["delatnost_ime"];
				
				if(in_array($adr,$adrese) === false){
					$adrese[] = $adr;
				}
				if(in_array($delnaz,$delatnosti) === false){
					$delatnosti[] = $delnaz;
				}
			}
		}
		//print_rarray($delatnosti);
		//print_rarray($adrese);
		echo "<strong>delatnosti:</strong><ul><li>".implode("</li><li>",$delatnosti)."</li></ul>";
		echo "<strong>adrese:</strong><ul>";
		foreach ($adrese as $a){
			$aa = $this->adrese->getAll($a);
			echo "<li>".$aa["lokacija"]["mesto"]["name"];
			if(strlen(trim($aa["ulica"]))>0){
				echo ", ".$aa["ulica"]." ".$aa["broj"];
			}
			echo "</li>";
		}
		echo "</ul>";
	}
	function reportKomercijalista(){
		$day = 24*60*60;
		
		if(isset($this->filter["datum"]) && preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$this->filter["datum"])){
			$tmp = explode(".",$this->filter["datum"]);
			$this->filter["datum"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
			if(isset($this->filter["datum1"]) && preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$this->filter["datum1"])){
				$tmp = explode(".",$this->filter["datum1"]);
				$this->filter["datum1"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
			}
		}else{
			if(isset($this->filter["datum1"]) && preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$this->filter["datum1"])){
				$tmp = explode(".",$this->filter["datum1"]);
				$this->filter["datum1"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
			}else{
				
				$tter = (isset($this->filter["teritorije"]) && strlen(trim($this->filter["teritorije"]))>0);
				$kkom = (isset($this->filter["komitent_id"]) && intval(trim($this->filter["komitent_id"]))>0);
				if($tter){
					//echo "1";
				}else{
					if($kkom){
						//echo "2";
					}else{
						//echo "3";
						$this->filter["datum"] = makeUnixTime(0,0,0,date("m"),date("d"),date("Y"));
					}
				}
				/*$this->filter["datum"] = makeUnixTime(0,0,0,date("m"),date("d"),date("Y"));
				$this->assign("filter_datum1",date("d.m.Y",$this->filter["datum"]));*/
			}
		}
		
		$this->filter["datum0"] = date2int($this->filter["datum0"]);
		$this->filter["datum10"] = date2int($this->filter["datum10"]);
		
		$this->assign("filter_datum",int2date($this->filter["datum"]));
		$this->assign("filter_datum1",int2date($this->filter["datum1"]));
		
		if ($this->filter["datum"]>0) {
			$this->assign("filter_datum_end",$this->filter["datum0"]>0?int2date($this->filter["datum0"]):int2date($this->filter["datum"]));
		}
		if ($this->filter["datum1"]>0) {
			$this->assign("filter_datum1_end",$this->filter["datum10"]>0?int2date($this->filter["datum10"]):int2date($this->filter["datum1"]));
		}
		
		$this->filter["kontaktiran"] = intval($this->filter["kontaktiran"]);
		switch ($this->filter["kontaktiran"]){
			case 1:
				$this->assign("selected_nekontaktiran"," selected");
				break;
			case 2:
				$this->assign("selected_kontaktiran"," selected");
				break;
		}
		$this->assign("filter_kd_start",$this->filter["kontaktiran_date_start"]);
		$this->assign("filter_kd_end",$this->filter["kontaktiran_date_end"]);
		
		$this->filter["kontaktiran_sastanak"] = (int)$this->filter["kontaktiran_sastanak"];
		switch ($this->filter["kontaktiran_sastanak"]){
			case 1:
				$this->assign("filter_ks_da"," selected");
				break;
			case 2:
				$this->assign("filter_ks_ne"," selected");
				break;
		}
		
		
		//$ter = $this->rokovnik->get_RokovnikTeritorije($this->filter["teritorija"],null,date("Y-m-d",$this->filter["datum"]));
		$teritorije = $this->filter["teritorije"];
		$tt = $this->filter["tt"];
		$td = $this->filter["td"];
		$ter = $this->rokovnik->get_RokovnikTeritorije();
			/*print_rarray($ter);*/
			$arr=array();
			foreach ($ter["polje_code"] as $k=>$code){
				$arr[$code][$ter["polje_id"][$k]] = $ter["polje_date"][$k];
			}
			foreach ($arr as $name=>$data){
				$ternames[] = $name;
				$tids=$tdates="";
				foreach ($data as $terid=>$terdate){
					$tids .= ",$terid";
					$tdates .= ",$terdate";
				}
				
				$terids[] = substr($tids,1);
				$tdts[] = substr($tdates,1);
				
				if($name == $tt){
					$ter_selected[] = " selected";
					$this->assign("filter_tids",substr($tids,1));
					$tmp = explode(",",substr($tdates,1));
					$html = "";
					foreach ($tmp as $date){
						$html .= "<option ter_id=\"".substr($tids,1)."\"";
						$tmpdate = explode("-",$date);
						$tmpdate = implode(".",array_reverse($tmpdate));
						if($tmpdate == $td){
							$html .= " selected";
						}
						$html .= ">";
						$tmp = explode("-",$date,3);
						$html .= implode(".",array_reverse($tmp));
						$html .= "</option>";
					}
				}else{
					$ter_selected[] = "";
				}
			}
			
			$this->assign("tn",$ternames);
			$this->assign("ts",$ter_selected);
			$this->assign("tds",$html);
			$this->assign("ti",$terids);
			$this->assign("td",$tdts);
			
		
			
		$polja = $this->rokovnik->get_RokovnikUsers($this->filter["radnik"]);
			$this->assign("radnik_id",$polja["polje_id"]);
			$this->assign("radnik_ime",$polja["polje_ime"]);
			$this->assign("radnik_prezime",$polja["polje_prezime"]);
			$this->assign("radnik_selected",$polja["polje_selected"]);
			$this->assign("radnik_user",$polja["polje_username"]);
		
		$polja = $this->rokovnik->get_RokovnikPoreklo($this->filter["poreklo"]);
			$this->assign("poreklo_id",$polja["polje_id"]);
			$this->assign("poreklo_name",$polja["polje_ime"]);
			$this->assign("poreklo_selected",$polja["polje_selected"]);
			
		$q = "SELECT rk.*, k.komitent_id, k.komitent_name, k.komitent_name1, p.name "
			."FROM rokovnik_komercijala rk, komitenti k, rokovnik_poreklo p "
			."WHERE rk.komitent=k.komitent_id "
			."AND rk.poreklo=p.id ";
		
		if(isset($this->filter["datum"]) && strlen(trim($this->filter["datum"]))>0){
			if ($this->filter["datum0"]>0) {
				$q .= "AND rk.poseta>=".$this->filter["datum"]." AND rk.poseta<".($this->filter["datum0"]+$day)." ";
			}else{
				$q .= "AND rk.poseta>=".$this->filter["datum"]." AND rk.poseta<".($this->filter["datum"]+$day)." ";
			}
		}
		if(isset($this->filter["datum1"]) && strlen(trim($this->filter["datum1"]))>0){
			if ($this->filter["datum10"]>0) {
				$q .= "AND rk.unos>=".$this->filter["datum1"]." AND rk.unos<".($this->filter["datum10"]+$day)." ";
			}else{
				$q .= "AND rk.unos>=".$this->filter["datum1"]." AND rk.unos<".($this->filter["datum1"]+$day)." ";
			}
		}
		if(isset($this->filter["teritorije"]) && strlen(trim($this->filter["teritorije"]))>0 && $this->filter["teritorije"]>0){
			$p = strpos($this->filter["teritorije"],",");
			$q .= "AND ";
			if($p>0){
				$tmp = explode(",",$this->filter["teritorije"]);
				$q .= "(";
				foreach ($tmp as $t){
					$q .= "teritorija=$t OR ";
				}
				$q = substr($q,0,strlen($q)-4).") ";
			}else{
				$q .= "teritorija=".$this->filter["teritorije"]." ";
			}
		}
		if(isset($this->filter["radnik"]) && $this->filter["radnik"]>0){
			$q .= "AND rk.user=".$this->filter["radnik"]." ";
		}
		if(isset($this->filter["poreklo"]) && $this->filter["poreklo"]>0){
			$q .= "AND rk.poreklo=".$this->filter["poreklo"]." ";
		}
		if(isset($this->filter["komitent_id"]) && intval($this->filter["komitent_id"])>0){
			$this->assign("filter_komitent",$this->filter["komitent"]);
			$this->assign("filter_komitent_id",$this->filter["komitent_id"]);
			$q .= "AND rk.komitent=".$this->filter["komitent_id"]." ";
		}
		$q .= "ORDER BY poseta, komitent_name";
		//echo $q;
		$res = $this->db->select2($q);
		if($res !== false){
			$sum = count($res);
			foreach ($res as $k=>$v){
				
				$skip=false;
				if ($this->filter["kontaktiran"]>0) {
					
					$sql = "SELECT 1 FROM rokovnik, rokovnik_result "
						  ."WHERE rokovnik.id=rokovnik_result.rokovnik_id "
						  ."AND komitent_id=".$v["komitent_id"]." ";
					
					if ($this->filter["kontaktiran_sastanak"]>0) {
						switch ($this->filter["kontaktiran_sastanak"]){
							case 1:
								$sql .= "AND rokovnik_result.type=3 ";
								break;
							case 2:
								$sql .= "AND rokovnik_result.type!=3 ";
								break;
						}
					}
					
					$fkds = date2int($this->filter["kontaktiran_date_start"]);
					$fkde = date2int($this->filter["kontaktiran_date_end"]);
					if ($fkde>0 && $fkde>0 && $fkde>=$fkds) {
						$sql .= "AND rokovnik.time>=$fkds AND rokovnik.time<".($fkde+$day)." ";
					}
						  
					$sql .= "LIMIT 1";
					
					$cc = $this->db->getOne($sql);
					$cc = $cc>0?true:false;
					switch ($this->filter["kontaktiran"]){
						case 1:
							$skip = $cc?true:false;
							break;
						case 2:
							$skip = $cc?false:true;
							break;
					}
				}
				
				
				if ($skip) {
					$sum--;
				}else{
					$ids[] = $v["id"];
					
					// kontakt
					$starttime = $this->filter["datum"]>0?$this->filter["datum"]:$v["poseta"];
					$endtime = $this->filter["datum1"]>0?$this->filter["datum1"]:0;
					$q = "SELECT 1 FROM rokovnik "
						."WHERE komitent_id=".$v["komitent_id"]
						." AND time>=$starttime";
					if ($endtime>0) {
						$q .= " AND time<=$endtime ";
					}
					$q .= " LIMIT 1";
					//echo "$q<br />";
					$ima = $this->db->getOne($q);
					if ($ima) {
						$classIma[] = "ima";
					}else{
						$classIma[] = "";
					}
					
					// komitent
					$kom_id[] = $v["komitent_id"];
					$komitent = trim($v["komitent_name"]);
					$komitent1 = trim($v["komitent_name1"]);
					if(mb_strtolower($komitent,"UTF-8") != mb_strtolower($komitent1,"UTF-8")){
						$komitent .= "<hr />$komitent1";
					}
					$kom[] = $komitent;
					
					// adrese
					if ($v["komitent_id"]>0) {
						$tmp = "";
						$adr = $this->db->select2("SELECT DISTINCT komdel_adresa_id FROM komitenti_delatnosti WHERE komdel_kom_id=".$v["komitent_id"]);
						if ($adr !== false) {
							foreach ($adr as $adrid){
								$a = $this->adrese->getAll($adrid["komdel_adresa_id"]);
								$tmp .= ", ".$a["ulica"]." ".$a["broj"]."<br />";
							}
						}
						$adrese[] = substr($tmp,2);
					}
					
					// radnik
					if($v["radnik"]>0){
						$q = "SELECT CONCAT(radnik_ime,' ',radnik_prezime) FROM radnici WHERE radnik_id=".$v["radnik"]." LIMIT 1";
						$rad[] = $this->db->getOne($q);
					}else{
						$rad[] = "";
					}
					
					//user
					$q = "SELECT radnik_ime, radnik_prezime FROM radnici, users WHERE users.radnik_id=radnici.radnik_id AND users.id=".$v["user"];
					$operater = $this->db->getRow($q);
					if($operater[0]){
						$operater_ime[] = $operater[0];
						$operater_prezime[] = " ".$operater[1];
					}else{
						$operater_ime[] = "";
						$operater_prezime[] = "";
					}
					
					// teritorija
					if($v["teritorija"]){
						$q = "SELECT code,`desc` FROM rokovnik_teritorije WHERE id=".$v["teritorija"];
						$ters = $this->db->getRow($q);
						$ter_name[] = $ters[0];
						$ter_desc[] = $ters[1];
						
					}else{
						$ter_name[] = "";
						$ter_desc[] = "";
					}
					
					if(date("H:i:s",$v["poseta"]) != "00:00:00"){
						$format = "d.m.Y H:i:s";
					}else{
						$format = "d.m.Y";
					}
					$pos[] = date($format,$v["poseta"]);
					$uno[] = date($this->getFromConfig("long_date"),$v["unos"]);
					if($v["zovi"]>0){
						$zov[] = date($this->getFromConfig("long_date"),$v["zovi"]);
					}else{
						$zov[] = "";
					}
					
					$por[] = $v["name"];
					
					// odgovor
					if($v["odgovor"]>0){
						$odg[] = $this->db->getOne("SELECT name FROM rokovnik_odgovor WHERE id=".$v["odgovor"]);
					}else{
						$odg[] = "";
					}
					
					$nap[] = nl2br($v["napomena"]);
				}
			}
			$this->assign("id",$ids);
			$this->assign("ima",$classIma);
			$this->assign("komitent_id",$kom_id);
			$this->assign("komitent",$kom);
			$this->assign("adresa",$adrese);
			$this->assign("radnik",$rad);
			$this->assign("operater_ime",$operater_ime);
			$this->assign("operater_prezime",$operater_prezime);
			$this->assign("teritorija",$ter_name);
			$this->assign("teritorija_desc",$ter_desc);
			$this->assign("poseta",$pos);
			$this->assign("unos",$uno);
			$this->assign("zovi",$zov);
			$this->assign("poreklo",$por);
			$this->assign("odgovor",$odg);
			$this->assign("napomena",$nap);
			
			$this->assign("info","$sum poseta");
		}else{
			$this->assign("info","0 poseta");
			$this->assign("start_comment","<!--");
			$this->assign("end_comment","-->");
			$this->assign("message","Nema rezultata");
		}
		
		echo $this->parseTemplate("rokovnik/report_komercijalista.tpl");
	}
	
	function exportKomercijalistaNezvani(){
		$ids = readValFromInput("ids");
		$ids = preg_replace('/<div.*?>.*?<\/div>/',"",$ids);
		$ids = explode(",",$ids);
		$novi = "sifra\tnaziv";
		foreach ($ids as $item){
			$item = html_entity_decode($item);
			$tmp = explode("<=>",$item,2);
			$id = trim($tmp[0]);
			$name = trim($tmp[1]);
			$name = nasaslova($name);
			
			$q = "SELECT 1 FROM rokovnik WHERE komitent_id=$id";
			$res = $this->db->getOne($q);
			if($res != 1){
				$novi .= "\n$id\t$name";
			}
		}
		$filename = "tmp".$_COOKIE["ypu"];
		
		file_put_contents($filename,$novi);
		echo $filename;
	}
	
	function reportTelemarketingZakazani(){
		
		$q = "SELECT k.komitent_name, rr.time as rrtime, rad.radnik_ime, rad.radnik_prezime, r.time, rk.name as kontakt, ro.name as odgovor "
			."FROM komitenti k, rokovnik r, rokovnik_result rr, users u, radnici rad, rokovnik_kontakt rk, rokovnik_odgovor ro "
			."WHERE r.komitent_id=k.komitent_id "
			."AND r.odgovor=ro.id "
			."AND r.kontakt = rk.id "
			."AND r.id=rr.rokovnik_id "
			."AND r.user_id=u.id "
			."AND u.radnik_id=rad.radnik_id "
			."AND rr.type=2 ";
		
		if(!isset($this->filter["datum"]) || strlen(trim($this->filter["datum"]))==0){
			$this->filter["datum"] = makeUnixTime(0,0,0,date("m"),date("d"),date("Y"));
		}else{
			$tmp = explode(".",$this->filter["datum"]);
			$this->filter["datum"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
		}
		$dan = 24*60*60;
		$q .= "AND (rr.time>".$this->filter["datum"]." AND rr.time<".($this->filter["datum"]+$dan)." ) ";
		if($this->filter["operater"]>0){
			$q .= "AND u.id=".$this->filter["operater"]." ";
		}
		
		$this->filter["komitent"] = trim($this->filter["komitent"]);
		if(strlen($this->filter["komitent"])>0){
			$q .= "AND k.komitent_name LIKE '%".$this->filter["komitent"]."%' ";
			$this->assign("filter_komitent",$this->filter["komitent"]);
		}
		
		$q .= "ORDER BY u.id, rr.time";
		$res = $this->db->select2($q);
		
		$this->assign("datum",date("d.m.Y",$this->filter["datum"]));
		
		if($res[0]){
			//print_rarray($res[0]);
			$info = count($res)." poziva";
			foreach ($res as $k=>$v){
				$row[] = $k+1;
				$kom[] = $v["komitent_name"];
				$rrtime[] = date("H:i",$v["rrtime"]);
				$radnik_ime[] = $v["radnik_ime"];
				$radnik_prezime[] = $v["radnik_prezime"];
				$time[] = date($this->getFromConfig("long_date"),$v["time"]);
				$kon[] = $v["kontakt"];
				$odgo[] = (strlen($v["odgovor"])>0)?": <i>\"".$v["odgovor"]."\"</i>":"";
			}
			$this->assign("row",$row);
			$this->assign("komitent",$kom);
			$this->assign("zak_time",$rrtime);
			$this->assign("radnik_ime",$radnik_ime);
			$this->assign("radnik_prezime",$radnik_prezime);
			$this->assign("call_time",$time);
			$this->assign("kontakt",$kon);
			$this->assign("odgovor",$odgo);
			$this->assign("rokovnikTMReportZak",$this->parseTemplate("rokovnik/report_telemarketing_zak_main.tpl"));
		}else{
			$this->assign("rokovnikTMReportZak","Nema rezultata");
		}
		
		$polja = $this->rokovnik->get_RokovnikUsers($this->filter["operater"]);
			$this->assign("user_id",$polja["polje_id"]);
			$this->assign("user_ime",$polja["polje_ime"]);
			$this->assign("user_prezime",$polja["polje_prezime"]);
			$this->assign("user_selected",$polja["polje_selected"]);
		
		$this->assign("info",$info);
		
		echo $this->parseTemplate("rokovnik/report_telemarketing_zak.tpl");
	}
	function reportTelemarketing(){
		$level = $this->getUserLevel();
		if ($level["name"]!="admin") {
			$this->assign("admin_start","<!--");
			$this->assign("admin_end","-->");
		}
		
		$q = "SELECT r.id, r.poreklo, r.kontakt, r.odgovor, k.komitent_id, k.komitent_name, rad.radnik_ime, rad.radnik_prezime, r.time as roktime, rp.name, rk.name, ro.name, r.napomena_special, r.napomena, rr.time as rrtime, rr.desc, rrt.name "
			."FROM rokovnik r, rokovnik_result rr, rokovnik_result_type rrt, rokovnik_poreklo rp, rokovnik_kontakt rk, rokovnik_odgovor ro, komitenti k, radnici rad, users u "
			."WHERE r.komitent_id=k.komitent_id "
			."AND r.user_id=u.id AND u.radnik_id=rad.radnik_id "
			."AND r.poreklo=rp.id "
			."AND r.kontakt=rk.id "
			."AND r.odgovor=ro.id "
			."AND r.id=rr.rokovnik_id "
			."AND rr.type=rrt.id ";
		//print_rarray($this->filter);
		// MKTIME
			if(isset($this->filter["end_date"]) && strlen(trim($this->filter["end_date"]))>0){
				$tmp = explode(".",$this->filter["end_date"]);
				$this->filter["end_date"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0]+1,$tmp[2]);
				if(isset($this->filter["start_date"]) || strlen(trim($this->filter["start_date"]))>0){
					$tmp = explode(".",$this->filter["start_date"]);
					$this->filter["start_date"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
				}else{
					$this->filter["start_date"]=-1;
				}
			}else{
				$this->filter["end_date"] = -1;
				if(isset($this->filter["start_date"]) && strlen(trim($this->filter["start_date"]))>0){
					$tmp = explode(".",$this->filter["start_date"]);
					$this->filter["start_date"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
				}else{
					$this->filter["start_date"] = makeUnixTime(0,0,0,date("m"),date("d"),date("Y"));
				}
			}
			
		//FILTERS
			// date
			$day = 24*60*60;
			if($this->filter["start_date"]>0){
				$q .= "AND r.time>=".$this->filter["start_date"]." ";
				$this->assign("start_date",date("d.m.Y",$this->filter["start_date"]));
			}
			if($this->filter["end_date"]>0){
				$q .= " AND r.time<=".$this->filter["end_date"]." ";
				$this->assign("end_date",date("d.m.Y",$this->filter["end_date"]-$day));
				//echo $this->filter["end_date"]-$day;
			}
			// user
			if($this->filter["user"]>0){
				$q .= "AND u.id=".$this->filter["user"]." ";
				
			}
			// komitent
			if(strlen(trim($this->filter["komitent"]))){
				$q .= "AND (";
				$q .= "k.komitent_name LIKE '%".trim($this->filter["komitent"])."%'";
				
				if(!preg_match('/[^\d]/',$this->filter["komitent"])){
					$q .= " OR k.komitent_id=".$this->filter["komitent"];
				}
				
				$q .= ") ";
				
				$this->assign("komitent",trim($this->filter["komitent"]));
			}
			// poreklo
			if($this->filter["poreklo"]>0){
				$q .= "AND r.poreklo=".$this->filter["poreklo"]." ";
			}
			// kontakt
			if($this->filter["kontakt"]>0){
				$q .= "AND r.kontakt=".$this->filter["kontakt"]." ";
			}
			// odgovor
			if($this->filter["odgovor"]>0){
				$q .= "AND r.odgovor=".$this->filter["odgovor"]." ";
			}
			// napomena
			$this->assign("display_napomena","none");
			if(isset($this->filter["napomena"]) && $this->filter["napomena"]>=0){
				if($this->filter["napomena"] == 1){
					$q .= "AND r.napomena!=''";
					$this->assign("napomena_ima"," selected");
					$this->assign("display_napomena","block");
					if (strlen(trim($this->filter["napomena_1"]))>0) {
						$q .= " AND r.napomena LIKE '%".addslashes(trim($this->filter["napomena_1"]))."%'";
						$this->assign("napomena_1",trim($this->filter["napomena_1"]));
					}
				}else{
					$q .= "AND r.napomena='' ";
					$this->assign("napomena_nema"," selected");
				}
			}
			// spec.dogadjaj
			if(isset($this->filter["napomena_spec"]) && $this->filter["napomena_spec"]>=0){
				if($this->filter["napomena_spec"] == 1){
					$q .= "AND r.napomena_special!=''";
					$this->assign("sd_ima"," selected");
				}else{
					$q .= "AND r.napomena_special='' ";
					$this->assign("sd_nema"," selected");
				}
			}
			// ishod
			if($this->filter["ishod"]>0){
				$q .= "AND rr.type=".$this->filter["ishod"]." ";
			}
		
		$q .= "ORDER BY r.id";
		//die($q);
		$res = $this->db->select2($q);
		$num = $res[0]?count($res):0;
		$this->assign("info","$num poziva");
		//echo $q;
		$polja = $this->rokovnik->get_RokovnikUsers($this->filter["user"]);
			$this->assign("user_id",$polja["polje_id"]);
			$this->assign("user_ime",$polja["polje_ime"]);
			$this->assign("user_prezime",$polja["polje_prezime"]);
			$this->assign("user_selected",$polja["polje_selected"]);
		$polja = $this->rokovnik->get_RokovnikPoreklo($this->filter["poreklo"]);
			$this->assign("poreklo_id",$polja["polje_id"]);
			$this->assign("poreklo",$polja["polje_ime"]);
			$this->assign("poreklo_selected",$polja["polje_selected"]);
		$polja_kontakt = $this->rokovnik->get_RokovnikKontakt($this->filter["kontakt"]);
			$this->assign("kontakt_id",$polja_kontakt["polje_id"]);
			$this->assign("kontakt",$polja_kontakt["polje_ime"]);
			$this->assign("kontakt_selected",$polja_kontakt["polje_selected"]);
		$polja = $this->rokovnik->get_RokovnikOdgovor($this->filter["odgovor"]);
			$this->assign("odgovor_id",$polja["polje_id"]);
			$this->assign("odgovor",$polja["polje_ime"]);
			$this->assign("odgovor_selected",$polja["polje_selected"]);
		$polja = $this->rokovnik->get_RokovnikResultTypes($this->filter["ishod"]);
			$this->assign("result_type_id",$polja["polje_id"]);
			$this->assign("result_type",$polja["polje_ime"]);
			$this->assign("result_type_selected",$polja["polje_selected"]);
			
		// DISPLAY REPORT
		if($num>0){
			$nepostojeci = array();
			foreach ($res as $k=>$v){
				$rows[] = $k+1;
				$ids[] = $v["id"];
				$kom_ids[] = $v["komitent_id"];
				$koms[] = $v["komitent_name"];
				$rad_ime[] = $v["radnik_ime"];
				$rad_pre[] = $v["radnik_prezime"];
				$roktime[] = ($v["roktime"]>0)?date($this->getFromConfig("long_date"),$v["roktime"]):"";
				$nap[] = (strlen($v["napomena"])>0)?'<a href="javascript:void(0)" onclick="alert(\''.str_replace("\n",'\\n',$v["napomena"]).'\'); event.stopPropagation()" title="Kliknite za pregled napomene"><img src="gui/icons/napomena.gif" /></a>':"";
				$napspec[] = (strlen($v["napomena_special"])>0)?'<a href="javascript:void(0)" onclick="alert(\''.str_replace("\n",'\\n',$v["napomena_special"]).'\'); event.stopPropagation()" title="Kliknite za pregled specijalnog događaja"><img src="gui/icons/napomena_special.gif" /></a>':"";
				//$napspec[] = $v["napomena_spec"];
				$ishod[] = $v["name"];
				$rrtime[] = ($v["rrtime"]>0)?date($this->getFromConfig("long_date"),$v["rrtime"]):"";
				$desc[] = $v["desc"];
				
				$kontaktname = $this->getName($polja_kontakt,$v["kontakt"]);
				$kontakti[$kontaktname][] = $v["komitent_name"];
				
			}
			
			//print_rarray($kontakti);
			
			$this->assign("row_id",$rows);
			$this->assign("id",$ids);
			$this->assign("komitent_id",$kom_ids);
			$this->assign("komitent_name",$koms);
			$this->assign("radnik_ime",$rad_ime);
			$this->assign("radnik_prezime",$rad_pre);
			$this->assign("roktime",$roktime);
			$this->assign("napomena",$nap);
			$this->assign("napomena_spec",$napspec);
			$this->assign("name",$ishod);
			$this->assign("rrtime",$rrtime);
			$this->assign("desc",$desc);
			
			$content = $this->parseTemplate("rokovnik/report_tm_main.tpl");
			
			$this->assign("rokovnikTMReport",$content);
		}else{
			$this->assign("rokovnikTMReport","Nema rezultata.");
		}
		
		echo $this->parseTemplate("rokovnik/report_telemarketing.tpl");
	}
	function reportManager(){
		$q = "SELECT k.komitent_id, k.komitent_name, k.komitent_adresa, r.user_id, r.time as call_time, rad.radnik_ime, rad.radnik_prezime, rr.time as rrtime, rs.user, rs.time, rs.time1, rs.napomena, rfb.name "
			."FROM komitenti k, radnici rad,rokovnik r, rokovnik_result rr, rokovnik_sastanak rs, rokovnik_feedback rfb "
			."WHERE r.komitent_id=k.komitent_id "
			."AND r.id=rr.rokovnik_id "
			."AND rr.id=rs.result_id "
			."AND rs.fb_id=rfb.id "
			."AND rs.radnik=rad.radnik_id ";
		
		if(	!preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$this->filter["start_date"]) &&
			!preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$this->filter["end_date"]) &&
			!preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$this->filter["start_date_odr"]) &&
			!preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$this->filter["start_date_odr"])
			){
			$this->filter["start_date"] = makeUnixTime(0,0,0,date("m"),date("d"),date("Y"));
		}else{
			$this->filter["start_date"] = date2int($this->filter["start_date"]);
			$this->filter["end_date"] = date2int($this->filter["end_date"]);
			$this->filter["start_date_odr"] = date2int($this->filter["start_date_odr"]);
			$this->filter["end_date_odr"] = date2int($this->filter["end_date_odr"]);
			
			/*
			// MKTIME unos
				if(isset($this->filter["end_date"]) && strlen(trim($this->filter["end_date"]))>0){
					$tmp = explode(".",$this->filter["end_date"]);
					$this->filter["end_date"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0]+1,$tmp[2]);
					if(isset($this->filter["start_date"]) || strlen(trim($this->filter["start_date"]))>0){
						$tmp = explode(".",$this->filter["start_date"]);
						$this->filter["start_date"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
					}else{
						$this->filter["start_date"]=-1;
					}
				}else{
					$this->filter["end_date"] = -1;
					if(isset($this->filter["start_date"]) && strlen(trim($this->filter["start_date"]))>0){
						$tmp = explode(".",$this->filter["start_date"]);
						$this->filter["start_date"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
					}else{
						$this->filter["start_date"] = makeUnixTime(0,0,0,date("m"),date("d"),date("Y"));
					}
				}
			// MKTIME zakazan
				if(isset($this->filter["end_date_odr"]) && strlen(trim($this->filter["end_date_odr"]))>0){
					$tmp = explode(".",$this->filter["end_date_odr"]);
					$this->filter["end_date_odr"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0]+1,$tmp[2]);
					if(isset($this->filter["start_date_odr"]) || strlen(trim($this->filter["start_date_odr"]))>0){
						$tmp = explode(".",$this->filter["start_date_odr"]);
						$this->filter["start_date_odr"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
					}else{
						$this->filter["start_date_odr"]=-1;
					}
				}else{
					$this->filter["end_date_odr"] = -1;
					if(isset($this->filter["start_date_odr"]) && strlen(trim($this->filter["start_date_odr"]))>0){
						$tmp = explode(".",$this->filter["start_date_odr"]);
						$this->filter["start_date_odr"] = makeUnixTime(0,0,0,$tmp[1],$tmp[0],$tmp[2]);
					}else{
						$this->filter["start_date_odr"] = makeUnixTime(0,0,0,date("m"),date("d"),date("Y"));
					}
				}*/
		}
		//print_rarray($this->filter);die();
		//FILTERS
			// date unos
			$day = 24*60*60;
			if($this->filter["start_date"]>0){
				$q .= "AND rs.time1>=".$this->filter["start_date"]." ";
				$this->assign("start_date",date("d.m.Y",$this->filter["start_date"]));
			}
			if($this->filter["end_date"]>0){
				$q .= " AND rs.time1<=".($this->filter["end_date"]+$day)." ";
				$this->assign("end_date",date("d.m.Y",$this->filter["end_date"]));
				//echo $this->filter["end_date"]-$day;
			}
			// date pozvan
			if($this->filter["start_date_odr"]>0){
				$q .= "AND rs.time>=".$this->filter["start_date_odr"]." ";
				$this->assign("start_date_odr",date("d.m.Y",$this->filter["start_date_odr"]));
			}
			if($this->filter["end_date_odr"]>0){
				$q .= " AND rs.time<=".($this->filter["end_date_odr"]+$day)." ";
				$this->assign("end_date_odr",date("d.m.Y",$this->filter["end_date_odr"]));
				//echo $this->filter["end_date"]-$day;
			}
			// user
			if($this->filter["user"]>0){
				$q .= "AND rs.user=".$this->filter["user"]." ";
				
			}
			// operater
			if($this->filter["operater"]>0){
				$sql = "SELECT id FROM users WHERE radnik_id=".$this->filter["operater"]." LIMIT 1";
				$userID = $this->db->getOne($sql);
				//echo $sql;
				if($userID){
					$q .= "AND r.user_id=$userID ";
				}
			}
			
			// radnik
			if($this->filter["radnik"]>0){
				$q .= "AND rs.radnik=".$this->filter["radnik"]." ";
			}
			// komitent
			if(strlen(trim($this->filter["komitent"]))){
				$q .= "AND k.komitent_name LIKE '%".trim($this->filter["komitent"])."%' ";
				$this->assign("komitent",trim($this->filter["komitent"]));
			}
			// napomena
			if(isset($this->filter["napomena"]) && $this->filter["napomena"]>=0){
				if($this->filter["napomena"] == 1){
					$q .= "AND rs.napomena!=''";
					$this->assign("napomena_ima"," selected");
				}else{
					$q .= "AND rs.napomena='' ";
					$this->assign("napomena_nema"," selected");
				}
			}
			// ishod
			if($this->filter["ishod"]>0){
				$q .= "AND rs.fb_id=".$this->filter["ishod"]." ";
			}
		
		$q .= "ORDER BY rs.id";
		//echo $q;
		
		$polja = $this->rokovnik->get_RokovnikUsers($this->filter["user"]);
			$this->assign("user_id",$polja["polje_id"]);
			$this->assign("user_ime",$polja["polje_ime"]);
			$this->assign("user_prezime",$polja["polje_prezime"]);
			$this->assign("user_selected",$polja["polje_selected"]);
		$polja = $this->rokovnik->get_Radnici($this->filter["radnik"]);
			$this->assign("radnik_id",$polja["polje_id"]);
			$this->assign("radnik_ime",$polja["polje_ime"]);
			$this->assign("radnik_prezime",$polja["polje_prezime"]);
			$this->assign("radnik_selected",$polja["polje_selected"]);
		$polja = $this->rokovnik->get_Radnici($this->filter["operater"]);
			$this->assign("operater_id",$polja["polje_id"]);
			$this->assign("operater_ime",$polja["polje_ime"]);
			$this->assign("operater_prezime",$polja["polje_prezime"]);
			$this->assign("operater_selected",$polja["polje_selected"]);
		$polja = $this->rokovnik->get_RokovnikFeedbacks($this->filter["ishod"]);
			$this->assign("fb_id",$polja["polje_id"]);
			$this->assign("fb_name",$polja["polje_ime"]);
			$this->assign("fb_selected",$polja["polje_selected"]);
			
		$res = $this->db->select2($q);
		$num = $res[0]?count($res):0;
		$this->assign("info","$num sastanaka");
		if($num>0){
			foreach ($res as $k=>$v){
				$rows[] = $k+1;
				$kom_ids[] = $v["komitent_id"];
				$koms[] = $v["komitent_name"];
				
				$adr = $this->adrese->getAll($v["komitent_adresa"]);
				$adrese[] = (strlen($adr["lokacija"]["mesto"]["ptt"])>0?$adr["lokacija"]["mesto"]["ptt"]." ":"").$adr["lokacija"]["mesto"]["name"].",<br />".$adr["ulica"]." ".$adr["broj"];
				
				$rad_ime[] = $v["radnik_ime"];
				$rad_pre[] = $v["radnik_prezime"];
				
				$u = $this->db->getRow("SELECT radnik_ime, radnik_prezime FROM radnici, users WHERE users.radnik_id=radnici.radnik_id AND users.id=".$v["user"]);
				$u_ime[] = $u[0];
				$u_pre[] = $u[1];
				
				$o = $this->db->getRow("SELECT radnik_ime, radnik_prezime FROM radnici, users WHERE users.radnik_id=radnici.radnik_id AND users.id=".$v["user_id"]);
				$o_ime[] = $o[0];
				$o_prezime[] = $o[1];
				
				
				$rrtime	= date($this->getFromConfig("long_date"),$v["rrtime"]);		// zakazan
				$time	= date($this->getFromConfig("long_date"),$v["time"]);		// odrzan
				$time1	= date($this->getFromConfig("long_date"),$v["time1"]);		// evidentiran
				$ctime	= date($this->getFromConfig("long_date"),$v["call_time"]);	// pozvan
				$vremena[] = "Pozvan:\\n$ctime\\n\\nZakazan:\\n$rrtime\\n\\nOdržan:\\n$time\\n\\nEvidentiran:\\n$time1";
				/*$rrtime[] = date($this->getFromConfig("long_date"),$v["rrtime"]);
				$time[] = date($this->getFromConfig("long_date"),$v["time"]);
				$time1[] = date($this->getFromConfig("long_date"),$v["time1"]);*/
				
				
				$nap[] = strlen(trim($v["napomena"]))>0?'<a href="javascript:void(0)" onclick="alert(\''.str_replace("\n",'\\n',trim($v["napomena"])).'\')"><img src="gui/icons/napomena.gif" /></a>':"";
				$ishod[] = $v["name"];
			}
			$this->assign("row_id",$rows);
			$this->assign("komitent_id",$kom_ids);
			$this->assign("komitent_name",$koms);
			$this->assign("komitent_adresa",$adrese);
			$this->assign("oime",$o_ime);
			$this->assign("oprezime",$o_prezime);
			$this->assign("rime",$rad_ime);
			$this->assign("rprezime",$rad_pre);
			$this->assign("uime",$u_ime);
			$this->assign("uprezime",$u_pre);
			
			//$this->assign("rtime",$rrtime);	// zakazan
			//$this->assign("time",$time);	// odrzan
			//$this->assign("time1",$time1);	// evidentiran
			$this->assign("vreme",$vremena);
			
			$this->assign("napomena",$nap);
			$this->assign("name",$ishod);
			
			$this->assign("rokovnikManReport",$this->parseTemplate("rokovnik/report_man_main.tpl"));
		}else{
			$this->assign("rokovnikManReport","Nema rezultata.");
		}
		
		echo $this->parseTemplate("rokovnik/report_manager.tpl");
	}
	
	function getName($polja,$id){
		foreach ($polja["polje_id"] as $k=>$v){
			if($v == $id){
				return $polja["polje_ime"][$k];
			}
		}
	}
}

?>